package com.woniuxy.service.user.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Role;
import com.woniuxy.entity.User;
import com.woniuxy.mapper.custom.CustomRoleMapper;
import com.woniuxy.mapper.custom.CustomUserMapper;
import com.woniuxy.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private CustomUserMapper userMapper;

    @Autowired
    private CustomRoleMapper roleMapper;

    @Override
    public int save(User user) {
        return userMapper.insertSelective(user);
    }

    @Override
    public int delete(Integer userId) {

        int rows=userMapper.revokeRoleFromUser(userId);

         rows += userMapper.deleteByPrimaryKey(userId);
        return rows;
    }

    @Override
    public User findOne(Integer userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        if (userId == null) {
            throw new RuntimeException("can't find User by:" + userId);
        }
        List<Integer> roleIds = userMapper.findRoleIdsByUserId(userId);

        List<Role> roles = new ArrayList<>();
        for (Integer roleId : roleIds) {
            Role role = roleMapper.selectByPrimaryKey(roleId);
            roles.add(role);
        }

        user.setRole(roles);

        return user;
    }

    @Override
    public List<User> findAll() {
        return userMapper.selectByExample(null);
    }

    @Override
    public int updateOne(User user) {
        int i = userMapper.updateByPrimaryKey(user);
        if (i > 0) {
            return i;
        }
        throw new RuntimeException("修改失败");
    }

    @Override
    public int update(User user) {

        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public List<User> findByLike(String str) {
        if (str == null) {
            throw new RuntimeException("数据获取失败");
        }
        String like = "'%" + str + "%'";
        List<User> users = userMapper.findByLike(like);
        return users;
    }

    @Override
    public PageInfo<User> findByPage(int page, int size) {

        PageHelper.startPage(page, size);

        List<User> users = userMapper.selectByExample(null);
        PageInfo<User> info=new PageInfo<>(users);

        return info;
    }


    @Override
    public List<User> Like(String str) {
        List<User> list = userMapper.findByLike(str);
        return list;
    }

    @Override
    public int distRoleToUser(Integer userId, List<Integer> roleIds) {
        int rows = userMapper.revokeRoleFromUser(userId);

        if (roleIds.size() < 1) {

            return rows;
        }

        rows += userMapper.distRoleToUser(userId, roleIds);
        return rows;
    }

    @Override
    public int revokeRoleFromUser(Integer userId) {
        return userMapper.revokeRoleFromUser(userId);
    }
}
